﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using XfTech.SmartTms.Data;
using XfTech.SmartTms.Entities;
using XfTech.SmartTms.IServices;
using Volo.Abp;
using XfTech.SmartTms.TodoItems.Dtos;

namespace XfTech.SmartTms.Services
{
    /// <summary>
    /// 待办事项接口，自己实现增删改查接口
    /// </summary>
    public class TodoAppService : SmartTmsAppService, ITodoAppService
    {
        //注入仓储接口
        private readonly ITodoRepository _todoItemRepository;

        //注入接口
        public TodoAppService(ITodoRepository todoItemRepository)
        {
            _todoItemRepository = todoItemRepository;
        }


        //实现三个方法，完成添加、查询、修改、删除
        //[RemoteService(IsEnabled =false)]  //禁止自动生成Api
        public async Task<TodoItemDto> CreateAsync(string text)
        {
            var todoItem = await _todoItemRepository.InsertAsync(
                  new TodoItem { Text = text }
              );

            return new TodoItemDto
            {
                Id = todoItem.Id,
                Text = todoItem.Text
            };
        }

        public async Task DeleteAsync(Guid id)
        {
            await _todoItemRepository.DeleteAsync(id);
        }

        public async Task<List<TodoItemDto>> GetListAsync()
        {
            var items = await _todoItemRepository.GetListAsync();
            return items.Select(item => new TodoItemDto
            {
                Id = item.Id,
                Text = item.Text
            }).ToList();
        }
      
    }
}
